package cn.edu.paisi.controller;


import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.edu.paisi.entity.Subject;
import cn.edu.paisi.entity.Test;
import cn.edu.paisi.service.ISubjectService;
import cn.edu.paisi.service.ITestService;
import cn.edu.paisi.util.RestResult;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author en.edu.paisi
 * @since 2021-03-09
 */
@RestController
@CrossOrigin
public class TestController {

	@Autowired
	private ITestService testService;
	@Autowired
	private ISubjectService subService;
	
	@RequestMapping("/autoTest")
	public RestResult<Test> autoTest(Test test,Integer counts){
		testService.save(test);
		Integer total = subService.getCounts(test.getTestId());
		int num=counts;
		if(counts!=0) {
			for(;num>0;num--) {
				Random random = new Random();
				int rand= random.nextInt(total-1)+1;
				Subject subject = subService.getById(rand);
				Test test2 = new Test();
				test2.setTestName(subject.getSubName());
				test2.setTestContent(subject.getSubMain());
				testService.save(test2);
			}
			return RestResult.ok("自动生成成功!");
		}else {
			return RestResult.failed("生成错误!");
		}
		
	}
}

